package com.phoenix.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.phoenix.entity.Events;
import com.phoenix.entity.EventsSigns;
import com.phoenix.entity.ResponseData;
import com.phoenix.entity.Users;
import com.phoenix.service.EventsService;
import com.phoenix.service.SignsService;
import com.phoenix.service.UserService;
import com.phoenix.util.MarkdownUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiresRoles(value = {"admin", "user"}, logical = Logical.OR)
@RequestMapping("/sign")
public class SignController {
    @Autowired
    EventsService eventsService;

    @Autowired
    SignsService signService;

    @Autowired
    UserService userService;

    @GetMapping("/signListForGroup")
    public String signListForGroup(Model model, @RequestParam(value = "keywords", defaultValue = "") String keywords, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        //进行分页，提供关键字查询
        //pageSize为分页的行数
        Map<String, Object> search = new HashMap<>();
        search.put("keywords", keywords);
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        model.addAttribute("signsTitle", "团体会员活动报名");
        search.put("type", "1");
        PageHelper.startPage(pageNum, pageSize);
        List<Events> list = signService.selectNSearch(search, user.getUserID());

        //使用PageHelper包装数据
        PageInfo<Events> pageInfo = new PageInfo<>(list);
        model.addAttribute("result", new ResponseData<>(200, "ok", pageInfo));
        model.addAttribute("keywords", keywords);
        return "signs/signsListForMembers";
    }

    @GetMapping("/signListForMember")
    public String signListForMember(Model model, @RequestParam(value = "keywords", defaultValue = "") String keywords, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        //进行分页，提供关键字查询
        //pageSize为分页的行数
        Map<String, Object> search = new HashMap<>();
        search.put("keywords", keywords);
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        model.addAttribute("signsTitle", "会员活动报名");
        search.put("type", "2");
        PageHelper.startPage(pageNum, pageSize);
        List<Events> list = signService.selectNSearch(search, user.getUserID());

        //使用PageHelper包装数据
        PageInfo<Events> pageInfo = new PageInfo<>(list);
        model.addAttribute("result", new ResponseData<>(200, "ok", pageInfo));
        model.addAttribute("keywords", keywords);
        return "signs/signsListForMembers";
    }

    @DeleteMapping("/unsign")
    @ResponseBody
    public ResponseData<String> unsign(Model model, @RequestParam Integer eventsID, @RequestParam String type) {
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        if (signService.unSignEvents(eventsID, user.getUserID(), type)) {
            return new ResponseData<>(200, "取消报名成功！", null);
        } else {
            return new ResponseData<>(500, "取消报名失败！", null);
        }
    }

    @PostMapping("/sign")
    @ResponseBody
    public ResponseData<String> sign(Model model, @RequestParam Integer eventsID, @RequestParam String type) {
        Subject currentUser = SecurityUtils.getSubject();
        String userName = (String) currentUser.getPrincipal();
        Users user = userService.selectUserByUsername(userName);
        if (signService.signEvents(eventsID, user.getUserID(), type)) {
            return new ResponseData<>(200, "报名成功！", null);
        } else {
            return new ResponseData<>(500, "报名失败！", null);
        }
    }

    @GetMapping("/signsCheck/{id}")
    public String signsCheck(Model model, @PathVariable Integer id) {
        Events events = eventsService.selectEventsByID(id);
        //markdown转html
        events.setEventsContent(MarkdownUtil.markdownToHtmlExtens(events.getEventsContent()));
        model.addAttribute("events", events);
        return "events/eventsCheck";
    }

    @GetMapping("/eventsSigns/{eventsID}")
    public String eventsSigns(Model model, @PathVariable Integer eventsID) {
        Events events = eventsService.selectEventsByID(eventsID);
        EventsSigns eventsSigns = signService.eventsSigns(eventsID);
        model.addAttribute("eventsSigns", eventsSigns);
        return "events/eventsSignsList";
    }
}
